package com.fishery.refrigeratory.controller;


import com.fishery.entity.Result;
import com.fishery.refrigeratory.entity.RefrigeratoryPrepareEnter;
import com.fishery.refrigeratory.entity.vo.RefrigeratoryPrepareEnterVo;
import com.fishery.refrigeratory.service.RefrigeratoryInInfoService;
import com.fishery.refrigeratory.service.RefrigeratoryPrepareEnterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Api(description = "冷库待入库信息接口")
@RestController
@CrossOrigin
@RequestMapping("/refrigeratoryEnter")
public class RefrigeratoryPrepareEnterController {

    @Autowired
    private RefrigeratoryPrepareEnterService refrigeratoryEnterService;

    @Autowired
    private RefrigeratoryInInfoService refrigeratoryInInfoService;

    /**
     * 新增待入库信息
     * @param refrigeratoryEnter
     * @return
     */
    @PostMapping("/in")
    @ApiOperation(value = "新增待入库信息(远程调用)")
    public Result saveRefrigeratoryEnter(@RequestBody RefrigeratoryPrepareEnter refrigeratoryEnter) {
        if (refrigeratoryEnter == null
                || StringUtils.isEmpty(refrigeratoryEnter.getProductId())
                || StringUtils.isEmpty(refrigeratoryEnter.getProductName())
                || StringUtils.isEmpty(refrigeratoryEnter.getProductWeight())
                || StringUtils.isEmpty(refrigeratoryEnter.getBelongRefrigeratoryId())
                || StringUtils.isEmpty(refrigeratoryEnter.getSuppliersSource())
                || StringUtils.isEmpty(refrigeratoryEnter.getSupplierName())
                || StringUtils.isEmpty(refrigeratoryEnter.getSupplierType())
        ){
            return Result.error("信息填写不全!");
        }
        return refrigeratoryEnterService.save(refrigeratoryEnter)? Result.success(): Result.error();
    }

    /**
     * 查看所有的待入库信息
     * @return
     */
    @GetMapping("/in")
    @ApiOperation(value = "查看所有的待入库信息")
    public Result getAllRefrigeratoryEnter() {
        return Result.success().data(refrigeratoryEnterService.list(null));
    }

    /**
     * 通过产品名称查看待入库信息
     * @param produceName
     * @return
     */
    @GetMapping("/get/{produceName}")
    @ApiOperation(value = "通过产品名称查看待入库信息")
    public Result getRefrigeratoryEnterByName(@PathVariable(value = "produceName", required = true)
                                                          @ApiParam(value = "产品名称") String produceName) {
        if (StringUtils.isEmpty(produceName)) {
            return Result.error();
        }
        return Result.success().data(refrigeratoryEnterService.getRefrigeratoryEnterByName(produceName));
    }


    @GetMapping("/in/{page}/{size}")
    @ApiOperation("分页查看所有待入库信息")
    public Result getPageAllRefrigeratoryEnter(@PathVariable(value = "page", required = true) Integer page,
                                               @PathVariable(value = "size", required = true) Integer size) {
        page = page <= 0? 1: page;
        size = size <= 0? 1: size;
        return Result.success().data(refrigeratoryEnterService.getPageAllRefrigeratoryEnter(page, size));
    }

    @PostMapping("/inRefrigeratory")
    @ApiOperation("指定待入库数量部分加入到其对应冷库")
    public Result enterRefrigeratory(RefrigeratoryPrepareEnterVo refrigeratoryPrepareEnterVo) {
        if (refrigeratoryPrepareEnterVo == null
            || StringUtils.isEmpty(refrigeratoryPrepareEnterVo.getProductWeight())
            || StringUtils.isEmpty(refrigeratoryPrepareEnterVo.getWarehousingPerson())
        ) {
            return Result.error("信息不全！无法添加进对应冷库");
        }
        return refrigeratoryEnterService.enterRefrigeratory(refrigeratoryPrepareEnterVo) != 0? Result.success(): Result.error();
    }

}
